package com.yksj.healthtalk.ui.chatting;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.FriendsListAdapter;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.InviteFriendListener;
import com.yksj.healthtalk.utils.DataParseUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 按条件邀请
 * @author zhao
 */
public class InviteConditionFragment extends Fragment implements InviteFriendListener {
	CustomerInfoEntity mInfoEntity;
	FriendsListAdapter mAdapter;
	PullToRefreshListView mPullToRefreshListView;
	
	int pageSize = 1;
	int pageNum = 20;
	final ObjectHttpResponseHandler mHandler = new ObjectHttpResponseHandler() {
		@Override
		public Object onParseResponse(String cotent) {
			return DataParseUtil.parseInviteResultData(cotent);
		}
		public void onSuccess(int statusCode, Object response) {
			if(response instanceof String){
				//邀请错误
				ToastUtil.showShort(getActivity(), String.valueOf(response));
			}else{
				mAdapter.onDataChange((List<CustomerInfoEntity>)response);
			}
		};
		public void onFinish() {
			LodingFragmentDialog.dismiss(getFragmentManager());
			mPullToRefreshListView.onRefreshComplete();
		};
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInfoEntity = getArguments().getParcelable("mCustomerInfoEntity");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.pull_to_refresh_for_end_layout,null);
		mPullToRefreshListView = (PullToRefreshListView)view.findViewById(R.id.listview);
		mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				onQueryData(++pageSize,pageNum);
			}
			
		});
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mAdapter = new FriendsListAdapter(getLayoutInflater(savedInstanceState),getActivity());
		mPullToRefreshListView.setAdapter(mAdapter);
		onQueryData(pageSize,pageNum);
	}

	private void onQueryData(int pageSize , int pageNum){
//		CUSTOMERID PAGESIZE PAGENUM SEX MINAGE MAXAGE AREA INTEREST
		if (SmartFoxClient.getLoginUserId() == null || mInfoEntity == null) {
			return;
		}
		JSONObject json  =  new JSONObject();
		try {
			json.put("CUSTOMERID", SmartFoxClient.getLoginUserId());
			json.put("PAGESIZE", pageSize);
			json.put("PAGENUM", pageNum);
			int sex;
			if (mInfoEntity.getSex().equals("")) {
				sex = 0;
			}else if(mInfoEntity.getSex().equalsIgnoreCase("W")){
				sex =2;
			}else {
				sex = 1;
			}
			json.put("SEX", sex);
			json.put("MINAGE", mInfoEntity.getMinAge());
			json.put("MAXAGE", mInfoEntity.getMaxAge());
			json.put("AREA",  mInfoEntity.getAreaCode());
			json.put("INTEREST", mInfoEntity.getInterestCode() == null ? "" : mInfoEntity.getInterestCode());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		HttpRestClient.doHttpQueryByCondition(json.toString(),mHandler);
	}
	
	@Override
	public List<String> onInvite() {
		return new ArrayList<String>(mAdapter.mChoosedMap.keySet());
	}
}
